package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.VersionDAO;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevReference;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.TenantProducts;
import hcn.base.Version;
import hcn.service.base.ITenantProductsService;
import hcn.service.base.IVersionService;
import hcn.service.base.qo.GetByTenantPCodeQo;
import hcn.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import pcn.bean.sign.VersionQo;
import pcn.enums.OperationTypeEmun;
import pcn.service.PcnOperLogService;
import pcn.service.PcnVersionService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SsdevService
public class PcnVersionServiceImpl implements PcnVersionService {

	@Autowired
	private VersionDAO versionDAO;

	@Autowired
	private IVersionService versionService;

	@Autowired
	@Qualifier("tenantProductsBean")
	private ITenantProductsService tenantProductsService;

	@SsdevReference("pcn.pcnOperLogService")
	private PcnOperLogService pcnOperLogService;

	@RpcService
	@Override
	public Map<String, Object> queryVersions(VersionQo qo) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("list", versionDAO.queryVersions(qo));
		resMap.put("total", versionDAO.countVersions(qo));
		return resMap;
	}

	@Override
	public Version queryVersionsWithoutToken(GetByTenantPCodeQo qo) throws ControllerException {
		if (CommonUtils.isEmpty(qo.getTenantId())) {
			throw new ControllerException("tenantId must not be empty");
		}
		if (CommonUtils.isEmpty(qo.getTenantPCode())) {
			throw new ControllerException("tenantPCode must not be empty");
		}
		if (CommonUtils.isEmpty(qo.getPlatform())) {
			throw new ControllerException("platform must not be empty");
		}
		List<Version> versions =
				versionDAO.findVersionByTenantPCodeAndPlatform(qo.getTenantPCode(),
						qo.getPlatform(),
						qo.getTenantId(), 0, 1);
		Version version = null;
		if (CommonUtils.isNotEmpty(versions)) {
			version = versions.get(0);
		}
		return version;
	}

	@RpcService
	@Override
	public Integer mdify(Version version) throws ControllerException {
		Integer id  = 0;
		if(version.getTenantPId() > 0){
			Map<String, Object> tenantproductsMap = tenantProductsService.getTenantProductsDetailById(version.getTenantPId());
			if(tenantproductsMap != null){
				TenantProducts tenantProducts = ConversionUtils.convert(tenantproductsMap, TenantProducts.class);
				version.setCloudId(tenantProducts.getCloudId());
				version.setProductId(tenantProducts.getProductId());
				version.setTenantPCode(tenantProducts.getTenantPCode());
				version.setTenantPName(tenantProducts.getTenantPName());
			}
		}
		if(version.getId() > 0){
			versionService.update(version);
			id = version.getId();
			//保存到记录表
			pcnOperLogService.saveInfo(version.getTenantPName(), OperationTypeEmun.update.getName(),version.getId()+"","App更新");
		}else{
			id = versionService.addVersion(version);
			//保存到记录表
			pcnOperLogService.saveInfo(version.getTenantPName(), OperationTypeEmun.add.getName(),version.getId()+"".toString(),"App更新");
		}

		return id;
	}
}
